home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / jpi / math.bas < prev    next >
Encoding:
BASIC Source File  |  1998-01-29  |  4.4 KB  |  132 lines

  1. Attribute VB_Name = "Math"
  2. Global SinTable(360) As Single
  3. Global CosTable(360) As Single
  4. Public Function MovePoint2D(PointToMove As Point2D, DirectionToMove) As Point2D
  5. Select Case DirectionToMove
  6. Case DIRECTION_LEFT
  7.   MovePoint2D.X = PointToMove.X - 1
  8.   MovePoint2D.Y = PointToMove.Y
  9. Case DIRECTION_UP
  10.   MovePoint2D.X = PointToMove.X
  11.   MovePoint2D.Y = PointToMove.Y - 1
  12. Case DIRECTION_RIGHT
  13.   MovePoint2D.X = PointToMove.X + 1
  14.   MovePoint2D.Y = PointToMove.Y
  15. Case DIRECTION_DOWN
  16.   MovePoint2D.X = PointToMove.X
  17.   MovePoint2D.Y = PointToMove.Y + 1
  18. End Select
  19. End Function
  20. Public Sub InitializeTables()
  21. For I = 0 To 360
  22.   SinTable(I) = Sin(I / (180 / 3.1415926))
  23.   CosTable(I) = -Cos(I / (180 / 3.1415926))
  24. Next I
  25. End Sub
  26. Function GetCollidePosition(LineObj As Line3D, ObjPosition As Point3D, ObjYaw As Integer, CollidePosition As Point3D) As Boolean
  27. Dim TempLine As Line3D
  28. Point1Yaw = Math.GetYawFromPoints(LineObj.Point1, ObjPosition)
  29. Point2Yaw = Math.GetYawFromPoints(LineObj.Point2, ObjPosition)
  30. If Math.IsYawWithinDirections(ObjYaw, Point1Yaw, Point2Yaw) = True Then
  31.   TempLine.Point1 = RotatePointAroundPoint(LineObj.Point1, ObjPosition, 360 - ObjYaw)
  32.   TempLine.Point2 = RotatePointAroundPoint(LineObj.Point2, ObjPosition, 360 - ObjYaw)
  33.   CollidePosition.Y = ((TempLine.Point1.Y - TempLine.Point2.Y) / (TempLine.Point2.X - TempLine.Point1.X)) * (TempLine.Point2.X)
  34.   CollidePosition = RotatePointAroundPoint(CollidePosition, ObjPosition, ObjYaw)
  35.   GetCollidePosition = True
  36. End If
  37. End Function
  38. Public Function RotatePointAroundPoint(Point1 As Point3D, PointToRotateAround As Point3D, Yaw As Integer) As Point3D
  39. X1 = Point1.X - PointToRotateAround.X
  40. Y1 = Point1.Y - PointToRotateAround.Y
  41. RotatePointAroundPoint.Y = (X1 * SinTable(Yaw) + Y1 * CosTable(Yaw)) + PointToRotateAround.Y
  42. RotatePointAroundPoint.X = (X1 * CosTable(Yaw) + Y1 * SinTable(Yaw)) + PointToRotateAround.X
  43. End Function
  44. Public Function IsYawWithinDirections(ObjYaw, Point1Yaw, Point2Yaw) As Boolean
  45. Yaw1 = ObjYaw + 180
  46. PYaw1 = Point1Yaw + 180
  47. PYaw2 = Point2Yaw + 180
  48. If Yaw1 < 360 Then Yaw1 = 360 - Yaw1
  49. If PYaw1 < 360 Then PYaw1 = 360 - PYaw1
  50. If PYaw2 < 360 Then PYaw2 = 360 - PYaw2
  51. If Yaw1 < PYaw2 Then
  52.   If Yaw1 > PYaw1 Then
  53.     IsYawWithinDirections = True
  54.     Exit Function
  55.   End If
  56. End If
  57. If Yaw1 < PYaw1 Then
  58.   If Yaw1 > PYaw2 Then
  59.     IsYawWithinDirections = True
  60.     Exit Function
  61.   End If
  62. End If
  63. End Function
  64. Public Function GetZIncline(X1, Y1, Z1, X2, Y2, Z2)
  65. Dim P1 As Point3D, P2 As Point3D
  66. P1.X = X1
  67. P1.Y = Y1
  68. P1.Z = Z1
  69. P2.X = X2
  70. P2.Y = Y2
  71. P2.Z = Z2
  72. GetZIncline = (Z1 - Z2) / GetDistance(P1, P2)
  73. End Function
  74. Public Function GetPercentage(MaxVal, VarVal) As Integer
  75. GetPercentage = VarVal * (100 / MaxVal)
  76. End Function
  77. Function GetYawFromPoints(Point1 As Point3D, Point2 As Point3D) As Integer
  78. GetYawFromPoints = GetYawFromXY(Point1.X, Point1.Y, Point2.X, Point2.Y)
  79. End Function
  80. Function GetYawFromXY(X1, Y1, X2, Y2) As Integer
  81. XDiff = X2 - X1
  82. YDiff = Y2 - Y1
  83. If XDiff = 0 Then XDiff = 1
  84. If YDiff = 0 Then YDiff = 1
  85. If XDiff > 0 Then
  86.   If YDiff > XDiff Then
  87.     Ratio = -((XDiff / YDiff) * 45) + 180
  88.   Else
  89.     YDiff = -YDiff
  90.     If YDiff > XDiff Then
  91.       Ratio = (90 + ((XDiff / YDiff) * 45)) - 90
  92.     Else
  93.       YDiff = -YDiff
  94.       Ratio = 90 + ((YDiff / XDiff) * 45)
  95.     End If
  96.   End If
  97. Else
  98.   XDiff = -XDiff
  99.   If YDiff > XDiff Then
  100.     Ratio = -((XDiff / YDiff) * 45) + 180
  101.   Else
  102.     YDiff = -YDiff
  103.     If YDiff > XDiff Then
  104.       Ratio = (90 + ((XDiff / YDiff) * 45)) - 90
  105.     Else
  106.       YDiff = -YDiff
  107.       Ratio = 90 + ((YDiff / XDiff) * 45)
  108.     End If
  109.   End If
  110.   Ratio = 360 - Ratio
  111. End If
  112. GetYawFromXY = Ratio
  113. End Function
  114. Public Function GetDistance(Point1 As Point3D, Point2 As Point3D) As Integer
  115. GetDistance = Sqr((Point1.X - Point2.X) ^ 2 + (Point1.Y - Point2.Y) ^ 2)
  116. End Function
  117. Public Function GetPropelCoordinates(ObjPos As Point3D, Yaw, ZInclination, Distance) As Point3D
  118. GetPropelCoordinates.X = ObjPos.X + (SinTable(Yaw) * Distance)
  119. GetPropelCoordinates.Y = ObjPos.Y + (CosTable(Yaw) * Distance)
  120. GetPropelCoordinates.Z = ObjPos.Z + ZInclination
  121. End Function
  122. Public Sub CheckVector(Vector As Vect3D)
  123. If Vector.Yaw < 1 Then Vector.Yaw = 360 + Vector.Yaw
  124. If Vector.Yaw > 360 Then Vector.Yaw = Vector.Yaw - 360
  125. End Sub
  126. Public Function CheckYaw(Yaw)
  127. CheckYaw = Yaw
  128. If Yaw < 1 Then CheckYaw = 360 + Yaw
  129. If Yaw > 360 Then CheckYaw = Yaw - 360
  130. End Function
  131.  
  132.